package com.android.job.logic;

import android.os.Handler;

import com.android.job.fusion.FusionCode;
import com.android.job.model.DownloadItem;
import com.android.job.service.request.DownloadFileRequest;
import com.android.job.util.FileHelper;
public class FileDonwloadLogic
{
	private static FileDonwloadLogic instance;
	
	private DownloadItem tempItem;

	private FileDonwloadLogic()
	{
		
	}

	public synchronized static FileDonwloadLogic getInstance()
	{
		if (instance == null)
		{
			instance = new FileDonwloadLogic();
		}

		return instance;
	}


	public void downloadUpdateFile(String name, String url, Handler handler,
			String savePath, int type)
	{
		if (null == url || 0 >= url.trim().length())
		{
			handler.sendMessage(handler.obtainMessage(FusionCode.ERROR_SERVER));
			return;
		}
		DownloadItem downItem = new DownloadItem();
		downItem.fileName = name;
		downItem.saveName = name;
		downItem.url = url;
		FileHelper.deleteFile(savePath + downItem.saveName);
		downItem.savePath = savePath + downItem.saveName;
		downItem.setHandler(handler);
		tempItem = downItem;
		DownloadFileRequest dfr = new DownloadFileRequest(url,type);
		dfr.setItem(downItem);
		dfr.setHandler(handler);
		tempItem.setTaskHandle(dfr.getJSONResponse());
	}

	public void cancelTask()
	{
		if (tempItem != null)
		{
			tempItem.getTaskHandle().cancel();
		}
	}
}
